﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CarBlackBox
{
    public partial class CommunicationFrame
    {
        public static byte[] TxFrame(CommunicationCommand Command, params object[] paramList)
        {
            byte[] Tx;            
            ushort paramLen;
            ushort len=非数据;
            switch (Command)
            {
                case CommunicationCommand.设置记录仪时钟:                    
                    paramLen=6;
                    len=(ushort)(7 + paramLen);
                    Tx = new byte[len];
                    DateTime date;
                    DateTime time;
                    date = (DateTime)paramList[0];
                    time = (DateTime)paramList[1];
                    Tx[数据位置 + 0] = WFNetLib.BytesOP.Byte2BCD((byte)(date.Year % 100));
                    Tx[数据位置 + 1] = WFNetLib.BytesOP.Byte2BCD((byte)date.Month);
                    Tx[数据位置 + 2] = WFNetLib.BytesOP.Byte2BCD((byte)date.Day);
                    Tx[数据位置 + 3] = WFNetLib.BytesOP.Byte2BCD((byte)time.Hour);
                    Tx[数据位置 + 4] = WFNetLib.BytesOP.Byte2BCD((byte)time.Minute);
                    Tx[数据位置 + 5] = WFNetLib.BytesOP.Byte2BCD((byte)time.Second);
                    break;
                case CommunicationCommand.设置车辆特征系数:
                    paramLen = 3;
                    len = (ushort)(7 + paramLen);
                    Tx = new byte[len];
                    uint Factor = (uint)paramList[0];
                    ushort h, l;
                    h = WFNetLib.BytesOP.GetHighShort(Factor);
                    l = WFNetLib.BytesOP.GetLowShort(Factor);
                    Tx[数据位置 + 0] = WFNetLib.BytesOP.GetLowByte(h);
                    Tx[数据位置 + 1] = WFNetLib.BytesOP.GetHighByte(l);
                    Tx[数据位置 + 2] = WFNetLib.BytesOP.GetLowByte(l);
                    break;
                case CommunicationCommand.设置记录仪中的车辆VIN号_车牌号码_分类:
                    paramLen = 41;
                    len = (ushort)(7 + paramLen);
                    Tx = new byte[len];
                    string vin=(string)paramList[0];
                    string license=(string)paramList[1];
                    string cartype = (string)paramList[2];
                    byte[] bVIN = WFNetLib.Strings.StringsFunction.getByteList_gb2312(vin);
                    for (int i = 0; i < 17 && i < bVIN.Length; i++)
                        Tx[数据位置 + i] = bVIN[i];
                    byte[] bLicense = WFNetLib.Strings.StringsFunction.getByteList_gb2312(license);
                    for (int i = 0; i < 12 && i < bLicense.Length; i++)
                        Tx[数据位置 + 17 + i] = bLicense[i];
                    byte[] bCarType = WFNetLib.Strings.StringsFunction.getByteList_gb2312(cartype);
                    for (int i = 0; i < 12 && i < bCarType.Length; i++)
                        Tx[数据位置 + 29 + i] = bCarType[i];                    
                    break;
                case CommunicationCommand.设置驾驶员代码_驾驶证号码:
                    paramLen = 21;
                    len = (ushort)(7 + paramLen);
                    Tx = new byte[len];
                    WFNetLib.Union_UInt32 DriverID = new WFNetLib.Union_UInt32();
                    DriverID.ofs_32 = Convert.ToUInt32(paramList[0]);
                    byte[] bDIN = WFNetLib.Strings.StringsFunction.getByteList_gb2312((string)paramList[1]);
                    for (int i = 0; i < 18; i++)
                    {
                        if (i < bDIN.Length)
                            Tx[数据位置 + i + 3] = bDIN[i];
                        else
                            Tx[数据位置 + i + 3] = 0;
                    }
                    Tx[数据位置 + 0] = DriverID.ofs1_16.ofs0_8;
                    Tx[数据位置 + 1] = DriverID.ofs0_16.ofs1_8;
                    Tx[数据位置 + 2] = DriverID.ofs0_16.ofs0_8;
                    break;
                default:
                    paramLen = 0;
                    len = (ushort)(7 + paramLen);
                    Tx = new byte[len];
                    break; 
            }            
            Tx[0] = TxHeader1;
            Tx[1] = TxHeader2;
            Tx[2] = (byte)Command;//命令
            Tx[3] = WFNetLib.BytesOP.GetHighByte(paramLen);//长度
            Tx[4] = WFNetLib.BytesOP.GetLowByte(paramLen);
            Tx[5] = SPARE;//备用字节
            Tx[len - 1] = WFNetLib.Verify.GetVerify_byteXOR(Tx, len - 1);
            return Tx;
        }
    }
}
